<?php
class CollegeManager
{
  private $_db;

  public function __construct($db)
  {
    $this->setDb($db);
  }

  public function add(College $college)
  {
    $q = $this->_db->prepare('INSERT INTO college SET nom = :nom, ville = :ville, cp = :cp, adresse = :adresse');

    $q->bindValue(':nom', $college->nom());
    $q->bindValue(':ville', $college->ville());
    $q->bindValue(':cp', $college->cp());
    $q->bindValue(':adresse', $college->adresse());

    $q->execute();
  }

  public function delete(College $college)
  {
    $this->_db->exec('DELETE FROM college WHERE id = '.$college->id());
  }

  public function get($id)
  {
    $id = (int) $id;

    $q = $this->_db->query('SELECT * FROM college WHERE id = '.$id);
    $donnees = $q->fetch();
    return new College($donnees);
  }

  public function getList()
  {
    $colleges = array();

    $q = $this->_db->query('SELECT * FROM college ORDER BY id');

    while ($donnees = $q->fetch())
    {
      $colleges[] = new College($donnees);
    }

    return $colleges;
  }

  public function update(College $college)
  {
    $q = $this->_db->prepare('UPDATE college SET nom = :nom, ville = :ville, cp = :cp, adresse = :adresse WHERE id = :id');

    $q->bindValue(':nom', $college->nom());
    $q->bindValue(':ville', $college->ville());
    $q->bindValue(':cp', $college->cp());
	$q->bindValue(':adresse', $college->adresse());
    $q->bindValue(':id', $college->id(), PDO::PARAM_INT);

    $q->execute();
  }

  public function setDb(PDO $db)
  {
    $this->_db = $db;
  }
}
?>